# -*- Python -*-

import os
import platform
import re
import subprocess
import site

try:
    import lit.formats
    import lit.util
except ImportError:
    pass

module_dir = getattr(config, 'module_dir', None)
if not module_dir:
    lit_config.fatal('Could not find the modules directory!')
site.addsitedir(module_dir)

# name: The name of this test suite.
config.name = 'Covert C++ Tools'

# Tweak PATH for Win32
if platform.system() == 'Windows':
    # Seek sane tools in directories and set to $PATH.
    path = getattr(config, 'lit_tools_dir', None)
    path = lit_config.getToolsPath(path,
                                   config.environment['PATH'],
                                   ['cmp.exe', 'grep.exe', 'sed.exe'])
    if path is not None:
        path = os.path.pathsep.join((path,
                                     config.environment['PATH']))
        config.environment['PATH'] = path

# Choose between lit's internal shell pipeline runner and a real shell.  If
# LIT_USE_INTERNAL_SHELL is in the environment, we use that as an override.
use_lit_shell = os.environ.get("LIT_USE_INTERNAL_SHELL")
if use_lit_shell:
    # 0 is external, "" is default, and everything else is internal.
    execute_external = (use_lit_shell == "0")
else:
    # Otherwise we default to internal on Windows and external elsewhere, as
    # bash on Windows is usually very slow.
    execute_external = (not sys.platform in ['win32'])

# testFormat: The test format to use to interpret tests.
#
# For now we require '&&' between commands, until they get globally killed and
# the test runner updated.
config.test_format = lit.formats.ShTest(execute_external)

# suffixes: A list of file extensions to treat as test files.
config.suffixes = ['.c', '.cpp']

# test_source_root: The root path where tests are located.
config.test_source_root = os.path.dirname(__file__)

llvm_tools_dir = getattr(config, 'llvm_tools_dir', None)
# Tweak the PATH to include the LLVM bin dir.
if not llvm_tools_dir:
    lit_config.fatal('Could not find the LLVM installation bin directory!')
config.environment['PATH'] = os.path.pathsep.join((llvm_tools_dir, config.environment['PATH']))

# Tweak the PATH to include the tools dir.
covert_tools_dir = getattr(config, 'covert_tools_dir', None)
if not covert_tools_dir:
    lit_config.fatal('No Covert C++ tools dir found!')
path = os.path.pathsep.join((covert_tools_dir, config.environment['PATH']))
config.environment['PATH'] = path

# Tweak the CPLUS_INCLUDE_PATH to include the Covert C++ headers
covert_include_dir = getattr(config, 'covert_include_dir', None)
if not covert_include_dir:
    lit_config.fatal('No Covert C++ headers dir found!')
try:
    include_path = os.path.pathsep.join((covert_include_dir, config.environment['CPLUS_INCLUDE_PATH']))
except KeyError:
    include_path = covert_include_dir
config.environment['CPLUS_INCLUDE_PATH'] = include_path

def findFileCheck(PATH):
    # Determine which FileCheck to use.
    FileCheckPath = os.getenv('FILECHECK_PATH')

    # If the user set FILECHECK in the environment, definitely use that and don't
    # try to validate.
    if FileCheckPath:
        config.environment['PATH'] = os.path.pathsep.join((FileCheckPath, config.environment['PATH']))

    # Otherwise look in the path.
    if not lit.util.which('FileCheck', PATH):
        lit_config.fatal("couldn't find 'FileCheck' program, try setting "
                         "FILECHECK in your environment")

cpp_std = getattr(config, 'cpp_std', None)
if not cpp_std:
    lit_config.fatal("No C++ standard specified")

findFileCheck(config.environment['PATH'])